package com.jinmdz.fmis.common.util;

import java.math.BigDecimal;

/**
 * 转换人民币大小金额
 * (源自网络)
 *
 * @author LiCongLu
 * @date 2020-04-10 13:55
 */
public class RenminbiUtil {
    /**
     * 金额转汉字大写
     *
     * @param charge 金额
     * @return
     * @author LiCongLu
     * @date 2020-04-10 14:04
     */
    public static String toRmb(BigDecimal charge) {
        //人民币大写金额形式
        String renMinBis = "";

        // 判断数字是否有效，为空或小于零返回空字符串
        if (charge == null || charge.compareTo(new BigDecimal(0)) < 0) {
            return renMinBis;
        }

        //将num取绝对值并四舍五入取2位小数
        charge = charge.setScale(2, BigDecimal.ROUND_HALF_UP);

        //0-9所对应的汉字
        String numberStr = "零壹贰叁肆伍陆柒捌玖";
        //数字位所对应的汉字
        String digitStr = "万仟佰拾亿仟佰拾万仟佰拾元角分";

        //将num乘100并转换成字符串形式
        String charges = charge.multiply(new BigDecimal(100))
                .stripTrailingZeros().toPlainString();

        //num的值乘以100的字符串长度
        int chargeLength = charges.length();
        int maxLength = 15;
        if (chargeLength > maxLength) {
            // 溢出
            return renMinBis;
        }

        //取出对应位数的str2的值。如：200.55,j为5所以str2=佰拾元角分
        digitStr = digitStr.substring(maxLength - chargeLength);

        //循环取出每一位需要转换的值
        for (int i = 0; i < chargeLength; i++) {
            //数字的汉语读法
            String numberChar = "";
            //数字位的汉字读法
            String digitChar = "";
            //用来计算连续的零值是几个
            int zero = 0;
            //取出需转换的某一位的值
            String chargeChar = charges.substring(i, i + 1);
            //转换为数字
            int temp = Integer.parseInt(chargeChar);
            if (i != (chargeLength - 3) && i != (chargeLength - 7) && i != (chargeLength - 11) && i != (chargeLength - 15)) {
                //当所取位数不为元、万、亿、万亿上的数字时
                if ("0".equals(chargeChar)) {
                    numberChar = "";
                    digitChar = "";
                    zero = zero + 1;
                } else {
                    if (!"0".equals(chargeChar) && zero != 0) {
                        numberChar = "零" + numberStr.substring(temp * 1, temp * 1 + 1);
                        digitChar = digitStr.substring(i, i + 1);
                        zero = 0;
                    } else {
                        numberChar = numberStr.substring(temp * 1, temp * 1 + 1);
                        digitChar = digitStr.substring(i, i + 1);
                        zero = 0;
                    }
                }
            } else {
                //该位是万亿，亿，万，元位等关键位
                if (!"0".equals(chargeChar) && zero != 0) {
                    numberChar = "零" + numberStr.substring(temp * 1, temp * 1 + 1);
                    digitChar = digitStr.substring(i, i + 1);
                    zero = 0;
                } else {
                    if (!"0".equals(chargeChar) && zero == 0) {
                        numberChar = numberStr.substring(temp * 1, temp * 1 + 1);
                        digitChar = digitStr.substring(i, i + 1);
                        zero = 0;
                    } else {
                        if ("0".equals(chargeChar) && zero >= 3) {
                            numberChar = "";
                            digitChar = "";
                            zero = zero + 1;
                        } else {
                            if (chargeLength >= 11) {
                                numberChar = "";
                                zero = zero + 1;
                            } else {
                                numberChar = "";
                                digitChar = digitStr.substring(i, i + 1);
                                zero = zero + 1;
                            }
                        }
                    }
                }
            }
            if (i == (chargeLength - 11) || i == (chargeLength - 3)) {
                //如果该位是亿位或元位，则必须写上
                digitChar = digitStr.substring(i, i + 1);
            }
            renMinBis = renMinBis + numberChar + digitChar;

            if (i == chargeLength - 1 && "0".equals(chargeChar)) {
                //最后一位（分）为0时，加上“整”
                renMinBis = renMinBis + '整';
            }
        }
        if (charge.compareTo(new BigDecimal(0)) == 0) {
            renMinBis = "零元整";
        }
        return renMinBis;
    }

    public static void main(String[] args) {
        BigDecimal bigDecimal = new BigDecimal(0);
        System.out.println(RenminbiUtil.toRmb(bigDecimal));
    }
}